{include file="common/header" /}
<link rel="stylesheet" href="__LAYUIMINI__/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
<style>
  body {
    background-color: #ffffff;
  }
  .layui-form-label {
      width: 150px;
  }
  .layui-input-block{
      margin-left: 150px;
  }
</style>

<body>
  <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>模型添加</legend>
  </fieldset>
  <div class="layui-form layuimini-form">
    {if condition="input('param.id')"}
    <input type="hidden" name="id" value="{:input('param.id')}">
    {/if}
    <div class="layui-form-item">
      <label class="layui-form-label">模型名称</label>
      <div class="layui-input-block" style="width:500px;">
        <input type="text" class="layui-input field-title" lay-verify="required" name="title" autocomplete="off"
          placeholder="请输入模型名称">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">表名</label>
      <div class="layui-input-block" style="width:500px;">
        <input {if condition="isset($data_info)" }disabled{/if} type="text" class="layui-input field-name"
          lay-verify="required" name="name" autocomplete="off" placeholder="请输入表名">
      </div>
    </div>
    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">描述</label>
      <div class="layui-input-block" style="width:610px;">
        <textarea name="description" placeholder="请输入描述" class="layui-textarea field-description"></textarea>
      </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否允许删除数据</label>
        <div class="layui-input-block">
            <div class="switch">
                <input type="checkbox" lay-filter="sw" name="isdel" lay-skin="switch" lay-text="是|否" data-name="isdel" data-check="1" data-nocheck="0" checked>
            </div>
            <input name="isdel" type="hidden" value="1">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否允许添加数据</label>
        <div class="layui-input-block">
            <div class="switch">
                <input type="checkbox" lay-filter="sw" name="isadd" lay-skin="switch" lay-text="是|否" data-name="isadd" data-check="1" data-nocheck="0" checked>
            </div>
            <input name="isadd" type="hidden" value="1">
        </div>
    </div>
    {if condition="!isset($data_info)"}
    <div class="layui-form-item">
      <label class="layui-form-label">新建表字段</label>
      <div class="layui-input-block">
        {volist name="$ex_res" id="vo"}
        <input type="radio" name="emptytable" value="{$vo.id}" title="{$vo.title}[{$vo.name}]" alt="{$vo.fields}"
          class="field-emptytable">
        {/volist}
      </div>
    </div>
    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">选择节点</label>
      <div class="layui-input-block" style="width: 400px;">
        <select name="pid" lay-verify="required" lay-search="">
          {volist name='tree' id='vo'}
          {if $vo.level <= 2 /} <option value="{$vo.id}" {if $vo.id==29 /} selected="" {/if}> {$vo.str | raw}
            {$vo.name}({$vo.level}级)</option>
            {/if}
            {/volist}
        </select>
      </div>
    </div>
    {/if}

    <div class="layui-form-item">
        <label class="layui-form-label">类型</label>
        <div class="layui-input-block">
          <input type="radio" name="type" value="1" title="列表显示" checked="">
          <input type="radio" name="type" value="2" title="单页显示">
        </div>
    </div>

    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn" lay-submit lay-filter="saveBtn">立即提交</button>
        <a href="{:url('index')}" class="layui-btn layui-btn-primary">返回</a>
      </div>
    </div>
  </div>

  <script>
    layui.use(['form', 'iconPickerFa', 'upload', 'laydate'], function () {
      var form = layui.form,
        iconPickerFa = layui.iconPickerFa,
        layer = layui.layer,
        upload = layui.upload,
        laydate = layui.laydate,
        $ = layui.$;

        form.on('switch(sw)', function(data){
            var obj=data.elem;
            if(data.elem.checked){
                var value=$(obj).attr('data-check');
            }else{
                var value=$(obj).attr('data-nocheck');
            }
            var name=$(obj).attr("data-name");
            $("input[name='"+name+"']").val(value);
        });

      //监听提交
      form.on('submit(saveBtn)', function (data) {
        var index = layer.load(1, {
          shade: [0.3, '#000'] //0.1透明度的白色背景
        });
        var data = data.field;
        // console.log(data)
        // return false
        $.ajax({
          url: "{:url('module/add')}",
          data: data,
          type: 'post',
          dataType: 'json',
          success: function (res) {
            // layer.close(index);
            // console.log(res)
            // return false
            if (res.errcode != 1) {
              layer.msg(res.message, { icon: 2, time: 2000 }, function () {
                layer.close(index);
              })
            } else {
              layer.msg(res.message, { icon: 1, time: 1000 }, function () {
                window.location.href = "{:url(CONTROLLER_NAME.'/index')}";
              })
            }
            layer.close(index);
          },
          error: function (XMLHttpRequest, textStatus, errorThrown) {
            layer.msg('网络失败，请刷新页面后重试!', { icon: 2, time: 2000 })
          }
        });

        return false;
      });

    });
  </script>
</body>

</html>